home *** CD-ROM | disk | FTP | other *** search
- (* ---------------------- begin argument module -------------------------- *)
-
- (* ArgLib.pas 16 March 1985. TURBO PASCAL version *)
-
- (* Get arguments from CP/M or MS-DOS Command Line, using UNIX conventions. *)
- (* Tested on Turbo Pascal ver 1 and 2; must compile to disk (not memory). *)
- (* Allows writing portable and/or UNIX-compatable programs on your micro. *)
- (* All compiler-dependencies are marked with the word "Turbo"; mods for *)
- (* changing among CP/M-80, CP/M-86, and MS-DOS are marked in "argv". *)
-
- (* CAUTION: CP/M and MS-DOS programs must finish ALL argcount and argv *)
- (* calls before doing ANY file operations. (Not required under UNIX.) *)
-
- (* Example: User calls program "cp" with two files: CP FILEA FILEB
- *
- * argcount --> 2
- * argv(1, ) --> 'FILEA '
- * argv(2, ) --> 'FILEB '
- *)
- (* EXPORT: ArgStrType, argcount, argv, resetOK *)
-
- (* author: Willett Kempton *)
-
- const
- MaxArgStrLen = 16; (* maximum characters per argument *)
- type
- ArgStrType = packed array[1..MaxArgStrLen]of char; (* file name argument *)
-
-
- procedure argv( argn : integer; (* requesting Nth argument *)
- var name : ArgStrType); (* returning its char image *)
- (* ARGument Value. Valid argn for files are 1..argcount. *)
- (* Note: argv(0, ) incorrectly returns blank; UNIX returns program name *)
- const
- MaxCmdLineLength = 122; (* bug: CP/M-80 Turbo v1 & v2 chops to 31 chars *)
- type
- CmdLineType = packed array [0..MaxCmdLineLength] of char;
- charset = set of char;
- var
- i, nextch, Start, ArgLen, CmdLen : integer;
- SYSdelim : charset; (* system file separators *)
- CmdLine : CmdLineType
- { CP/M-86 Turbo } { absolute DSeg : $80 ; }
- { MS-DOS Turbo } absolute CSeg : $80 ;
- { CP/M-80 Turbo } { absolute $80 ; }
-
- procedure skip (delims:charset);
- (* skip past either delimiters or arguments. *)
- begin
- while (CmdLine[nextch] in delims) and (nextch <= CmdLen) do
- nextch := nextch + 1
- end (* skip *);
-
- begin (* argv *)
- CmdLen := ord(CmdLine[0]); (* length of command line *)
- {if CmdLen > 31 then } { CP/M-80 Turbo }
- { begin writeln('Command line too long.'); Halt end;} { CP/M-80 Turbo }
- SYSdelim :=
- { CP/M } [' ',',',';','[',']','=','<','>' ,'/','_' ];
- { MS-DOS } { [' ',',',';','[',']','=','<','>' ]; } (* more?? *)
- nextch := 1;
- Start := 1;
- for i := 1 to argn do
- begin
- skip(SYSdelim) (* skip leading delimiters *);
- Start := nextch (* overwriting all but last value *);
- skip([chr(0)..chr(127)]-SYSdelim) (* skip argument *);
- { Turbo bug prohibits [chr(0)..chr(255)]; thus 8-bit chars disallowed }
- end;
- ArgLen := nextch-Start;
- (* now use Start and ArgLen to set the string *)
- if ArgLen > MaxArgStrLen then ArgLen := MaxArgStrLen;
- for i:= 1 to ArgLen do name[i] := CmdLine[Start-1+i];
- for i:= (ArgLen+1) to MaxArgStrLen do name[i] := ' '; (* blank fill *)
- end (* argv *);
-
-
- function argcount : integer;
- (* ARGument COUNT: Number of arguments on command line *)
- (* This is slow, so call it just once and set an integer variable. *)
- var
- c: integer;
- ArgStr: ArgStrType;
- begin
- c := 0;
- repeat
- c := c+1;
- argv(c, ArgStr);
- until (ArgStr[1] = ' ');
- argcount := c - 1;
- end (* argcount *);
-
- function argc : integer;
- (* Count of all arguments, including program name. Preferred call is
- "argcount"; this is retained for compatability with earlier version. *)
- begin
- argc := argcount + 1;
- end;
-
-
- function resetOK (var f: text; name: ArgStrType) : boolean;
- (* Associate name with file variable. Return true if file exists and
- is nonempty. *)
- begin
- assign(f,name); { Turbo, non-standard }
- {$I- Turbo: turn off I/O checks, or reset and eof() can cause crash. }
- reset(f);
- resetOK := (IOresult = 0); { Turbo, non-standard }
- {$I+ Turbo: restore I/O checks }
- end (* resetOK *);
-
- (* ------------------------ end argument module ------------------------- *)